public class ReverseNode1 {
	public static void main(String[] args) {
		Node node1 = new Node(1);
		Node node2 = new Node(2);
		Node node3 = new Node(3);
		Node node4 = new Node(4);
//		Node node5 = new Node(5);
//		Node node6 = new Node(6);
//		Node node7 = new Node(7);
//		Node node8 = new Node(8);
//		Node node9 = new Node(9);
		node1.next = node2;
		node2.next = node3;
		node3.next = node4;
//		node4.next = node5;
//		node5.next = node6;
//		node6.next = node7;
//		node7.next = node8;
//		node8.next = node9;
		Node rs = reverse(node1);
		while (rs != null) {
			System.out.print(rs.value + "->");
			rs = rs.next;
		}
	}

	private static Node reverse(Node node) {
		Node head = null;
		Node tail = null;
		Node tmp = node;
		int i = 0;
		Node head1 = null;
		Node tail1 = null;
		while (tmp != null) {
			if (i == 0) {
				head = tmp;
			}
			tail1 = tmp;
			tmp = tmp.next;
			if (i == 2) {
				reverse(head, tail1);
				if (head1 == null) {
					head1 = tail1;
				}
				if (tail != null) {
					tail.next = tail1;
				}
				tail = head;
				i = -1;
				head = null;
			}
			i++;
		}
		if (head != null) {
			reverse(head, tail1);
			if (tail == null) {
				return tail1;
			} else {
				tail.next = tail1;
			}
		}
		return head1;
	}

	private static Node reverse(Node node, Node tail) {
		Node tmp = null;
		Node n = node;
		Node n1 = null;
		while (n != null && n != tail) {
			n1 = n.next;
			n.next = tmp;
			tmp = n;
			n = n1;
		}
		tail.next = tmp;
		return tail;

	}

	static class Node {
		public Node(int value) {
			this.value = value;
		}

		int value;
		Node next;

	}
}
